<template>
	<view class="container">
		<view class="bg1">
			<view></view>
		</view>
		<view class="dian"></view>
		<view class="header">
			<image src="https://www.guizhouqy.com/mini/back.png" @click="back" mode=""></image>
			<text>设置</text>
			<image src="" mode=""></image>
		</view>
		<view class="list-cell b-b">
			<text class="cell-tit">头像</text>
			<view class="cell-more" @click="changeAvatar">
				<image :src="form.avatar" style="width:32px;height: 32px;"/>
			</view>
		</view>
		<view class="list-cell b-b">
			<text class="cell-tit">昵称</text>
			<view class="cell-more">
				<uni-easyinput class="uni-mt-5" :inputBorder="false" suffixIcon="compose" v-model="form.nickname" style="text-align: right;" :styles="nickStyle"></uni-easyinput>
			</view>
		</view>
		<view class="list-cell b-b">
			<text class="cell-tit">手机号</text>
			<view class="cell-more " @click="setPhone">点击修改 <uni-icons type="forward" size="14" color="#FFFFFF"></uni-icons></view>
		</view>
		<view class="list-cell b-b">
			<text class="cell-tit">修改密码</text>
			<view class="cell-more " @click="setPassword">点击修改 <uni-icons type="forward" size="14" color="#FFFFFF"></uni-icons></view>
		</view>
		<view class="list-cell b-b">
			<text class="cell-tit">当前版本</text>
			<text class="cell-tip">{{ version }}</text>
			<text class="cell-more"></text>
		</view>
		<view class="list-cell log-out-btn" @click="save">
			<text class="cell-tit">保存修改</text>
		</view>
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">退出登录</text>
		</view>
	</view>
</template>

<script>
	import {
		BASE_URL,
		HEADER,
		TOKEN
	} from '@/config/app';
	import store from '@/store';
	import {
		goLogin,
		loginOut
	} from '@/libs/login';
	
	export default {
		data() {
			return {
				nickStyle: {
					color: '#FFFFFF'
				},
				version: '', // 版本
				form: {
					avatar: 'https://qcloud.dpfile.com/pc/S7BJsbEfiUw8Rlsa9wnylLOVFNy5VRVTVXX8vW-iHIOv-GHOx_pUinGa_Fy-bwpn.jpg',
					nickname: ''
				}
			};
		},
		mounted() {
			this.getUserInfo()
		},
		methods:{
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			// 获取用户信息
			async getUserInfo() {
				let res = await this.$api.user.getUserInfo.get()
				let userInfo = res.data.userInfo
				this.version = res.data.version
				
				this.form.avatar = userInfo.avatar
				this.form.nickname = userInfo.nickname
			},
			// 保存用户信息
			async save() {
				uni.showLoading({
					title: '保存中...'
				});
				let res = await this.$api.user.update.post(this.form)
				uni.hideLoading();
				if (res.code == 0) {
					this.$tool.success(res.msg)
				} else {
					this.$tool.error(res.msg)
				}
			},
			// 头像上传
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (file) => {
						uni.showLoading({
							title: '上传中...'
						});
						uni.uploadFile({
							url: BASE_URL + '/api/Common/uploadFile',
							filePath: file.tempFilePaths[0],
							name: 'file',
							success: (result) => {
								uni.hideLoading();
								let res = JSON.parse(result.data)
								if (res.code != 0) {
									this.$tool.error(res.msg)
									return 
								}
								this.form.avatar = res.data.url
							}
						});
					}
				});
			},
			// 设置新手机号
			setPhone() {
				uni.navigateTo({
					url: '/pages/set/phone'
				}) 
			},
			// 设置密码
			setPassword() {
				uni.navigateTo({
					url: '/pages/set/password'
				}) 
			},
			// 退出
			toLogout() {
				uni.showLoading({
					title: '操作中...'
				});
				if (loginOut()) {
					uni.removeStorageSync('uid')
					uni.hideLoading();
					this.$tool.success('退出成功')
					setTimeout(() => {
						goLogin()
					}, 800)
					
				}
			},
		}
	}
</script>

<style lang='scss'>
	page .container{
		background: #240F41;
		color: #FFFFFF;
		min-height: 100vh;
		padding: 0 26rpx;
		
		.bg1 {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			position: absolute;
			left: calc(50%-59px);
		
			view {
				width: 118px;
				height: 118px;
				background: linear-gradient(90deg, #C829F3 0%, #4516CA 100%);
				box-shadow: inset 0rpx 8rpx 8rpx 0rpx rgba(0, 0, 0, 0.25);
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				opacity: 0.9;
				filter: blur(90px);
			}
		}
		
		.dian {
			width: 118px;
			height: 118px;
			background: linear-gradient(90deg, #1F87EF 0%, #1F87EF 100%);
			box-shadow: inset 0rpx 8rpx 8rpx 0rpx rgba(0, 0, 0, 0.25);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			opacity: 0.9;
			filter: blur(90px);
			position: absolute;
			left: 0;
			top: 20px;
		}
		
		.header {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding-top: 30px;
			margin-bottom: 26rpx;
		
			image {
				width: 48rpx;
				height: 48rpx;
				z-index: 999;
			}
		}
	}
	.list-cell{
		width: 100%;
		height: 88rpx;
		background: linear-gradient( 180deg, rgba(154,36,185,0.2) 0%, rgba(133,22,233,0.2) 100%);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 2rpx solid #9A24B9;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 16rpx;
		padding: 16rpx;
		
		&.log-out-btn{
			margin-top: 40upx;
			background: linear-gradient(313deg, #7116E9 0%, #15D1E2 100%);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: none;
			.cell-tit{
				color: #FFFFFF;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			font-size:$font-sm;
			color:#FFFFFF;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-sm;
			color: #FFFFFF;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-sm;
			color: #FFFFFF;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
	.uni-easyinput__content-input {text-align: right;}
</style>
